//
//  BusinessDetailView.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 16/6/7.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "BusinessDetailView.h"
#import "BusinessViewCell.h"
#import "DetailsFirstCell.h"
#import "DetailsSecondCell.h"
#import "BusinessDetailModel.h"
#import "RecommendedController.h"
#import "BusMapLocationController.h"

@interface BusinessDetailView()<UITableViewDataSource,UITableViewDelegate>
{
    MBProgressHUD *progressHUD;
    NSString *_phoneStr;
    //有无商家产品
    BOOL _haveProduct;
}


@property(nonatomic,strong)SDCycleScrollView *cycleScrollView;
@property(nonatomic,strong)BusinessDetailModel *detailModel;

@end

@implementation BusinessDetailView

static NSString *const DetailsFirstIdentifier = @"DetailsFirstIdentifier";
static NSString *const DetailsSecondIdentifier = @"DetailsSecondIdentifier";

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    if (self = [super initWithFrame:frame style:style]) {
        
        self.delegate = self;
        self.dataSource = self;
        
    }
    return self;
}

- (void)setBussinessId:(NSString *)bussinessId
{
    _bussinessId = bussinessId;
    
    
    //菊花加载
    progressHUD = [MBProgressHUD loadingInformationToView];
    [self addSubview:progressHUD];
    
    //商家详情
    [self detailsLoadData];
    //商家产品
    [self businessProductsData];
    
}

#pragma mark >>>>>>>>>>网络请求
//商家详情
- (void)detailsLoadData
{
    NSString *shopid = _bussinessId;
    
    NSDictionary *param = @{@"id":[NSString stringWithFormat:@"%@", shopid]};
    NSLog(@"%@",param);
    
    
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:param AndMenthodName:@"api/Business/info" completeBlock:^(id object) {
        
                NSLog(@"%@",object);
        //code = 1 查询成功
        if (JudgeBackDataCode) {
            NSDictionary *tempDict = [object objectForKey:@"data"];
            _detailModel = [[BusinessDetailModel alloc] initWithDictionary:tempDict error:nil];
            [progressHUD hide:YES];
            
        }
        
        [self reloadData];
        
    }];
    
}

//商家产品
- (void)businessProductsData
{
    NSString *shopid = _bussinessId;
    
    NSDictionary *param = @{@"shopId":[NSString stringWithFormat:@"%@", shopid]};
    
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:param AndMenthodName:@"/api/Business/shopBusinessList" completeBlock:^(id object) {
        
        if (JudgeBackDataCode) {
            _haveProduct = true;
        }
        [self reloadData];
        
    }];
    
}


#pragma mark>>>>>>>>>>>数据源 >>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 3;
    }else if (section == 1){
        return 2;
    }else{
        return 1;
    }
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DetailsSecondIdentifier];
    
    if (cell==nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:DetailsSecondIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    if (indexPath.section == 0) {
        
        if (indexPath.row == 0) {
            
            DetailsFirstCell *cell = [tableView dequeueReusableCellWithIdentifier:DetailsFirstIdentifier];
            if (cell==nil) {
                
                cell = [[DetailsFirstCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DetailsFirstIdentifier];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            //商家名
            cell.detailsName.text = _detailModel.title;
            cell.detailsNum.text = [NSString stringWithFormat:@"%d分",_detailModel.score];
            
            return cell;
            
        }else if (indexPath.row ==1){
            //地址
            cell.imageView.image = [UIImage imageNamed:@"shangjia_3"];
            cell.textLabel.text = _detailModel.dizhi;
            cell.textLabel.numberOfLines= 0;
            return cell;
            
        }else{
            
            DetailsSecondCell *cell = [DetailsSecondCell loadDetailsSecondCellView];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.numLabel.font = FONT(14);
            cell.numLabel.textColor = DEFAULT_COLOR;
            if (_isWeChatDetails) {
                
                [cell.detailsCall setImage:[UIImage imageNamed:@"icon_wechat"] forState:UIControlStateNormal];
                cell.numLabel.text = _detailModel.weixin;
                
                
            }else{
                
                
                [cell.detailsCall setImage:[UIImage imageNamed:@"shangjia_2"] forState:UIControlStateNormal];
                NSString *phoneStr = _detailModel.dianhua;
//                _phoneStr = [phoneStr substringToIndex:11];
                
                
                cell.numLabel.text = phoneStr;
            }
            
            return cell;
            
        }
        
    }else if (indexPath.section == 1) {
        
        if (indexPath.row == 0) {
            
            cell.textLabel.text = @"经营范围";
            cell.detailTextLabel.text = @"";
            cell.textLabel.font =[UIFont systemFontOfSize:14];
            cell.accessoryType = UITableViewCellAccessoryNone;
            return cell;
            
        }else{
            //经营范围
            cell.textLabel.text = _detailModel.zycp;
            cell.textLabel.textColor = [UIColor grayColor];
            cell.textLabel.font =[UIFont systemFontOfSize:13];
            cell.textLabel.numberOfLines = 3;
            return cell;
        }
        
    }else {
        
//        if (indexPath.row == 0) {
            cell.textLabel.text = @"商家产品";
            cell.detailTextLabel.text = @"点击更多";
            
            cell.detailTextLabel.text = _haveProduct >= 1 ? @"点击更多":@"暂无商品";
            cell.detailTextLabel.font =[UIFont systemFontOfSize:14];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.textLabel.font =[UIFont systemFontOfSize:14];
            return cell;
//        }else{
//            
//            UITableViewCell *cell = [[UITableViewCell alloc] initWithFrame:CGRectMake(0,0,WIDTH,HEIGHT/13)];
//            
//            UIImageView *image = [[UIImageView alloc] init];
//            
//            image.image = _haveProduct >= 1 ? [UIImage imageNamed:@"businessPush.jpg"] : [UIImage imageNamed:@"haveProduct.jpg"];
//            
//            image.frame = CGRectMake(0,0,WIDTH,HEIGHT/13);
//            [cell.contentView addSubview:image];
//            
//            return cell;
//        }
        
    }
    
}




#pragma mark>>>>>>>>>>>>代理
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld",indexPath.row);
   
    
    if (indexPath.section == 0) {
        
        if (indexPath.row == 1) {
            //map
            BusMapLocationController *mapVC = [[BusMapLocationController alloc] init];
            mapVC.location = _detailModel.dizhi;
            mapVC.title = _detailModel.title;
            [self.parentVC.navigationController pushForm:self.parentVC.navigationController pushToAndHidden:mapVC];
            
        }else if (indexPath.row == 2){
            //拨电话
            NSString *phoneNumStr = [NSString stringWithFormat:@"tel://%@",_detailModel.dianhua];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumStr]];
        }
    }else if (indexPath.section == 2){
        //商家产品
        if (_haveProduct) {
            NSString *businessIdStr = [NSString stringWithFormat:@"%@",_bussinessId];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"BusinessDetailToProductBusinessVC" object:nil userInfo:@{@"businessId":businessIdStr}];
        }
    }
}

//微信号复制
-(BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath

{
    
    if (indexPath.section == 0 && indexPath.row ==2) {
        
        return YES;
        
    }
    
    return NO;
    
}

-(BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender

{
    
    if (action == @selector(copy:)) {
        return YES;
    }
    return NO;
    
}

-(void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender

{
    
    if (action == @selector(copy:)) {
        
        DetailsSecondCell *cell = (DetailsSecondCell *)[tableView cellForRowAtIndexPath:indexPath];
        [UIPasteboard generalPasteboard].string =cell.numLabel.text;
        
    }
    
}

- ( UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        
        NSArray *jypicArr = _detailModel.jypic;
        
        if (!jypicArr.count)return nil;
        
        NSMutableArray *imageArr = [NSMutableArray array];
        for (NSString *urlStr  in jypicArr) {
            [imageArr addObject:urlStr];
        }
        
        //        NSLog(@"%@",imageArr);
        
        SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0,0,WIDTH,HEIGHT/5) delegate:nil placeholderImage:[UIImage imageNamed:@"bg_loadCycle.jpg"]];
        self.cycleScrollView = cycleScrollView;
        cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
        cycleScrollView.imageURLStringsGroup = imageArr;
        cycleScrollView.pageDotColor = [UIColor whiteColor];
        cycleScrollView.currentPageDotColor = DEFAULT_COLOR;
        return cycleScrollView;
        
    
    }
    return nil;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        if (indexPath.row == 0) {
            return HEIGHT/12;
        }else{
            return HEIGHT/14;
        }
        
    }else if(indexPath.section == 1){
        
        if (indexPath.row == 0) {
            return HEIGHT/18;
        }else{
            return HEIGHT/12;
        }
    }else{
        return HEIGHT/13;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return HEIGHT/3;
    }
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}







@end
